import {
  createRouter,
  createWebHistory
} from 'vue-router'
import Layout from '../components/Layout/index.vue'
import Login from '../views/Login/index.vue'
import Home from '../views/Home/index.vue'
import Audit from '../views/Audit/index.vue'
import Publisted from '../views/Published/index.vue'
import RecycleBin from '../views/RecycleBin/index.vue'
import Banner from '../views/Banner/index.vue'
import dataVisualize from '../views/dataVisualize/index.vue'
const routes = [
  {
    path: '/',
    redirect: 'login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/layout',
    name: 'layout',
    component: Layout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: Home
      },
      {
        path: 'audit',
        name: 'audit',
        component: Audit
      },
      {
        path: 'publish',
        name: 'publish',
        component: Publisted
      },
      {
        path: 'draft',
        name: 'draft',
        component: RecycleBin
      },
      {
        path: 'bannerList',
        name: 'bannerList',
        component: Banner
        
      },
      {
        path: 'dataVisualize',
        name: 'dataVisualize',
        component: dataVisualize
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 判断有没有登录
  if (!localStorage.getItem('token')) {
    if (to.name == "login") {
      next();
    } else {
      router.push('login')
    }
  } else {
    next();
  }
  // if (!store.state.userToken) { //未登录
  //   if (to.name == 'login') {
  //     next()
  //   } else {
  //     next('/login')
  //   }
  // } else {
  //   //跳转之前获取用户的路由信息(权限信息)
  //   console.log(store.state,'9999')
  //   if (store.state.permission.length === 0) {
  //     // if (!store.state.permission) {
  //     store.dispatch('getUserPerssion').then(()=>{
  //       next({ ...to, replace: true })
  //     })

  //     // next()
  //   } else {
  //     next()
  //   }
  // }


  // if (!store.state.userToken) {
  //   if (to.name == 'login') {
  //     next()
  //   } else {
  //     next('/login')
  //   }
  // } else {
  //   if (store.state.permission.length == 0) {
  //     store.dispatch('getUserPerssion').then(res => {
  //       console.log(res)
  //       next()
  //     })
  //   } else {
  //     next()
  //   }
  // }

});

export default router